package com.example.teesystem.controller;

import com.example.teesystem.common.Permission.PermissionCheck;
import com.example.teesystem.common.exception.CustomException;
import com.example.teesystem.common.utils.PermissionUtil;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.common.valid.MapContains;
import com.example.teesystem.common.valid.ValidErrorMessage;
import com.example.teesystem.entity.vo.knowledge.KnowledgeUpdateVo;
import com.example.teesystem.entity.vo.knowledge.KnowledgeUploadVo;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.service.KnowledgeService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/tee/knowledge",produces = "application/json; charset=utf-8")
@RequiredArgsConstructor
@Validated
public class KnowledgeController {
    private final KnowledgeService knowledgeService;
    
    /**
     * 添加知识点
     * @param knowledgeUploadVo 知识点信息
     * @return
     */
    @PostMapping("/add")
    @PermissionCheck(PermissionUtil.COURSE_INFO_EDIT)
    public Response add(@Valid @RequestBody(required = false) KnowledgeUploadVo knowledgeUploadVo) {
        if(knowledgeService.add(knowledgeUploadVo)) {
            return Response.ok();
        } else {
            throw new CustomException(ResultCode.ADD_INFO_FAIL);
        }
    }
    
//    /**
//     * 添加知识点列表
//     * @param knowledgeUploadVos 知识点信息
//     * @return
//     */
//    @PostMapping("/addList")
//    public Response add(@Valid @RequestBody(required = false) List<KnowledgeUploadVo> knowledgeUploadVos) {
//        for (KnowledgeUploadVo knowledgeUploadVo : knowledgeUploadVos) {
//            if (!PermissionUtil.permissionCheck(MyHandlerInterceptor.getUid(), PermissionUtil.KNOWLEDGE_MANAGE, knowledgeUploadVo.getCourseId()))
//                return Response.error(ResultCode.NO_PERMISSION);
//        }
//        if(knowledgeService.add(knowledgeUploadVos)) {
//            return Response.ok();
//        } else {
//            throw new CustomException(ResultCode.ADD_INFO_FAIL);
//        }
//    }
    
    /**
     * 更新知识点
     * @param knowledgeUpdateVo 知识点信息
     * @return
     */
    @PostMapping("/update")
    @PermissionCheck(PermissionUtil.COURSE_INFO_EDIT)
    public Response update(@Valid @RequestBody(required = false) KnowledgeUpdateVo knowledgeUpdateVo) {
        if(!PermissionUtil.permissionCheck(MyHandlerInterceptor.getUid(), PermissionUtil.KNOWLEDGE_MANAGE, knowledgeUpdateVo.getCourseId())) {
            return Response.error(ResultCode.NO_PERMISSION);
        }
        if(knowledgeService.update(knowledgeUpdateVo)) {
            return Response.ok();
        } else {
            throw new CustomException(ResultCode.UPDATE_INFO_FAIL);
        }
    }
    
    /**
     * 获取知识点体系
     * @param map 传入课程 id
     * @return
     */
    @PostMapping("/get")
    public Response get(
            @MapContains(keys = {"courseId"}, message = ValidErrorMessage.COURSE_ID_VALID_FAIL)
            @RequestBody Map<String, Integer> map) {
        Integer courseId = map.get("courseId");
        return Response.ok(knowledgeService.getChapterKnowledge(courseId));
    }
    
    /**
     * 知识点删除，若有子结点则无法删除
     * @param map 传入知识点 id
     * @return
     */
    @PostMapping("/delete")
    public Response delete(
            @MapContains(keys = {"id"}, message = ValidErrorMessage.COURSE_ID_VALID_FAIL)
            @RequestBody Map<String, Integer> map) {
        if(knowledgeService.delete(map.get("id"))) {
            return Response.ok();
        } else {
            throw new CustomException(ResultCode.DELETE_INFO_FAIL);
        }
    }
}
